# -*- coding = utf-8 -*-
# @Time : 2021/12/22 16:16
# @Author : 龙王赘婿_彪少
# @File : demo10.py
# @Software: PyCharm

# 控件
# 选框控件
# 按钮，可框选的按钮，带参数的信号与槽连接，槽函数中确定发送信号的对象
# 滑动条
# 进度条 定时器
# 日历控件


import sys
from PyQt5.QtWidgets import (QWidget, QCheckBox,
                             QApplication, QPushButton,
                             QFrame, QSlider, QLabel,
                             QProgressBar, QCalendarWidget)
from PyQt5.QtCore import Qt, QBasicTimer, QDate
from PyQt5.QtGui import QColor, QPixmap


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        # self.init_01()
        # self.init_02()
        # self.init_03()
        # self.init_04()
        self.init_05()
        self.SHOW()

    def SHOW(self):
        self.setGeometry(350, 350, 300, 150)
        self.setWindowTitle("QCheckBox")
        self.show()

    def init_01(self):
        cb = QCheckBox("Show title", self)
        cb.move(20, 20)
        # 切换，执行两次就相当于没执行
        cb.toggle()
        # 带参数传递的信号与槽的连接
        cb.stateChanged.connect(self.changeTitle)

    # 槽函数
    def changeTitle(self, state):
        print(state)
        # Qt.Checked 就是Cpp中的枚举体变量
        if state == Qt.Checked:
            self.setWindowTitle("QCheckBox")
        else:
            self.setWindowTitle("")

    def init_02(self):
        self.col = QColor(0, 0, 0)

        redb = QPushButton("Red", self)
        # 设置这个参数，按键变成可点的框框
        redb.setCheckable(True)
        redb.move(10, 10)
        # PyQt中，带参数的信号与槽连接的方式
        redb.clicked[bool].connect(self.setColor)

        greenb = QPushButton("Green", self)
        greenb.setCheckable(True)
        greenb.move(10, 60)

        # 表示传递一个bool参数
        greenb.clicked[bool].connect(self.setColor)

        blueb = QPushButton("Blue", self)
        blueb.setCheckable(True)
        blueb.move(10, 110)

        blueb.clicked[bool].connect(self.setColor)

        self.square = QFrame(self)
        self.square.setGeometry(150, 20, 100, 100)
        self.square.setStyleSheet("QWidget{background-color: %s}" % self.col.name())

    def setColor(self, pressed):
        # 调用此函数，返回发送信号的对象，这个真有用，学Qt的时候还没学到
        source = self.sender()

        print(pressed)

        if pressed:
            val = 255
        else:
            val = 0

        if source.text() == "Red":
            self.col.setRed(val)
        elif source.text() == "Green":
            self.col.setGreen(val)
        else:
            self.col.setBlue(val)

        self.square.setStyleSheet("QWidget{background-color: %s}" % self.col.name())

    def init_03(self):
        # 设置枚举体参数 和父类
        sld = QSlider(Qt.Horizontal, self)
        # 设置焦点
        sld.setFocusPolicy(Qt.NoFocus)
        sld.setGeometry(30, 40, 100, 30)
        sld.valueChanged[int].connect(self.changeValue)

        self.label = QLabel(self)
        self.label.setGeometry(160, 40, 80, 30)
        self.label.setText("0")

    def changeValue(self, value):
        self.label.setText(str(value))

    def init_04(self):
        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 600, 2000, 25)

        self.btn = QPushButton("Start", self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        # 原来QBasicTimer就是字面意思
        self.timer = QBasicTimer()
        self.step = 0

    def timerEvent(self, e):
        if self.step >= 100:
            self.step = 0

        self.step = self.step+1
        self.pbar.setValue(self.step)

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText("start")
        else:
            self.timer.start(500, self)
            self.btn.setText("stop")

    def init_05(self):
        cal = QCalendarWidget(self)
        cal.setGridVisible(True)
        cal.move(20, 20)
        cal.clicked[QDate].connect(self.showDate)

        self.lbl = QLabel(self)
        date = cal.selectedDate()
        self.lbl.setText(date.toString())
        self.lbl.move(130, 260)

    def showDate(self, date):
        self.lbl.setText(date.toString())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # python好慢，下面函数执行了肉眼可见的时间
    ex = Example()
    sys.exit(app.exec_())
